package com.chenjl.transport.codec;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.chenjl.serialization.Serialization;
import com.chenjl.serialization.support.HessianSerialization;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.handler.codec.MessageToByteEncoder;
/**
 * 对象编码器
 * 2016-8-30 18:33:00
 * @author chenjinlong
 */
@Sharable
public class ObjectEncoder extends MessageToByteEncoder<Object> {
	private static final Logger logger = LoggerFactory.getLogger(ObjectEncoder.class);
	private Serialization serialization = new HessianSerialization();
	
	@Override
	protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception {
		logger.info("encode , hashcode : {}, msg class : {}",this.hashCode(),msg.getClass());
		
		byte[] bytes = serialization.toByte(msg);
		out.writeBytes(bytes);
	}
}